import { Injectable } from '@angular/core';

import { configOptionDto, DriverDto, DriverEngine, DriverOperationDto, modeDto, ruleDriverRelationDto } from 'ispace_de'
import { WindowsManagerService } from './windows-manager.service';
import { getFileDownloadUrl } from 'ispace.core.main';
@Injectable({
  providedIn: 'root'
})
export class DriveEnginService {

  public driveEngine!: DriverEngine;

  constructor(private windowManager: WindowsManagerService) {
      
    this.driveEngine = DriverEngine.init();

    // step 1: 配置驱动
    let opt:configOptionDto = new configOptionDto(); 
    
    // 配置
    opt.cfg = new configOptionDto(); 

    // 模式
    opt.modes = [];
    let mode = new modeDto();
    mode.id = 1;
    mode.name = "资源管理器";
    mode.main = (url,data,source)=>{ 
       this.windowManager.open("资源管理器",url,{iconUrl: getFileDownloadUrl("Desktop/.ispace/images/drive_icons/folder.png"),title: source?.name}); 
       };
    opt.modes.push(mode);
    let mode2 = new modeDto();
    mode2.id = 2;
    mode2.name = "属性";
    mode2.main = (url,data,source)=>{   
      this.windowManager.open("属性",url,{width: data.window.width,height: data.window.height,iconUrl: source?.iconUrl,title: "属性-" + source?.name}); 
    };
    opt.modes.push(mode2);

    let mode3 = new modeDto();
    mode3.id = 3;
    mode3.name = "文本编辑器";
    mode3.main = (url,data,source)=>{  this.windowManager.open("文本编辑器",url,{iconUrl: source?.iconUrl,title: source?.name});  };
    opt.modes.push(mode3);

     // 模式
     let mode6 = new modeDto();
     mode6.id = 6;
     mode6.name = "窗口";
     mode6.main = (url,data,source)=>{ this.windowManager.open((data?.window?.title??"窗口")+"-"+source?.name,url,{iconUrl: source?.iconUrl,width: data.window.width,height: data.window.height});  };
     opt.modes.push(mode6);
 

    // 驱动
    opt.drivers = [];
    // 映射
    opt.maps = [];



/***** hyperlink  ****/

// http://127.0.0.1/app/hyperlink/index.html?address=

    // 模式
    let mode4 = new modeDto();
    mode4.id = 4;
    mode4.name = "超链接";
    mode4.main = (url)=>{  window.open(url,"_blank"); };
    opt.modes.push(mode4);

    // 驱动
    let driver3 = new DriverDto();
    driver3.id = 4;
    driver3.name = "hyperlink";
    driver3.fileIconUrl = getFileDownloadUrl("Desktop/.ispace/images/drive_icons/link.png");
    driver3.operation = []; 
    // 驱动-操作： hyperlink打开
      let operation3 = new DriverOperationDto();
      operation3.driverId = driver3.id;
      operation3.id = 1;
      operation3.name = "打开"; 
      operation3.mode = 4;
      operation3.entry = "/app/hyperlink/index.html?path=${path}&type=${type}";
      driver3.operation.push(operation3); 
    opt.drivers.push(driver3);
    // 驱动-编辑：
    let operation4 = new DriverOperationDto();
    operation4.driverId = driver3.id;
    operation4.id = 2;
    operation4.data = {window:{ width: '400px',height: '250px',title: '编辑超链接'}};
    operation4.name = "编辑"; 
    operation4.mode = 6;
    operation4.entry = "/app/hyperlink/edit.html?path=${path}&type=${type}";
    driver3.operation.push(operation4); 
  opt.drivers.push(driver3);
   // 映射
    let map4 = new ruleDriverRelationDto();
        map4.rule = /.*\.link$/;
        map4.isDir = false;
        map4.driverId = 4;
    opt.maps.push(map4);




  // // ****  fileTransfer 文件传输app配置-上传 ****

  // 驱动
    let driver_fileTransfer = new DriverDto();
    driver_fileTransfer.id = 6;
    driver_fileTransfer.name = "文件传输";
    driver_fileTransfer.fileIconUrl = getFileDownloadUrl("Desktop/.ispace/images/drive_icons/upload.png");
    driver_fileTransfer.operation = []; 
    // 驱动-操作： 上传
      let operation_fileTransfer = new DriverOperationDto();
      operation_fileTransfer.driverId = driver_fileTransfer.id;
      operation_fileTransfer.id = 1;
      operation_fileTransfer.name = "上传"; 
      operation_fileTransfer.data = {window:{ width: '700px',height: '500px',title: '上传'}};
      operation_fileTransfer.mode = 6;
      operation_fileTransfer.entry = "/app/file-transfer/upload.html?path=${path}&type=${type}";
      driver_fileTransfer.operation.push(operation_fileTransfer);  
    opt.drivers.push(driver_fileTransfer);
   // 映射
    let map_fileTransfer2 = new ruleDriverRelationDto();
        map_fileTransfer2.rule =/.*/;
        map_fileTransfer2.isDir = true;
        map_fileTransfer2.driverId = driver_fileTransfer.id;
    opt.maps.push(map_fileTransfer2); 

    

  // // ****  fileTransfer 文件传输app配置-下载 ****

  // 驱动
    let driver_fileTransfer_download = new DriverDto();
    driver_fileTransfer_download.id = 7;
    driver_fileTransfer_download.name = "文件传输-下载";
    driver_fileTransfer_download.fileIconUrl = getFileDownloadUrl("Desktop/.ispace/images/drive_icons/download.png");
    driver_fileTransfer_download.operation = [];  
        // 驱动-操作： 下载
      let operation_fileTransfer_download = new DriverOperationDto();
      operation_fileTransfer_download.driverId = driver_fileTransfer_download.id;
      operation_fileTransfer_download.id = 2;
      operation_fileTransfer_download.name = "下载"; 
      operation_fileTransfer_download.data = {window:{ width: '700px',height: '500px',title: '下载'}};
      operation_fileTransfer_download.mode = 6;
      operation_fileTransfer_download.entry = "/app/file-transfer/download.html?path=${path}&type=${type}";
      driver_fileTransfer_download.operation.push(operation_fileTransfer_download);  
    opt.drivers.push(driver_fileTransfer_download);
   // 映射
    let map_fileTransfer_download = new ruleDriverRelationDto();
        map_fileTransfer_download.rule =/.*/;
        map_fileTransfer_download.isDir = false;
        map_fileTransfer_download.driverId = driver_fileTransfer_download.id;
    opt.maps.push(map_fileTransfer_download); 
    let map_fileTransfer_download2 = new ruleDriverRelationDto();
        map_fileTransfer_download2.rule =/.*/;
        map_fileTransfer_download2.isDir = true;
        map_fileTransfer_download2.driverId = driver_fileTransfer_download.id;
    opt.maps.push(map_fileTransfer_download2); 


/***** 资源管理器  ****/

 
    let driver = new DriverDto();
    driver.id = 1;
    driver.name = "资源管理器";
    driver.fileIconUrl = getFileDownloadUrl("Desktop/.ispace/images/drive_icons/folder.png"); 
    driver.operation = []; 
    // 驱动-操作： 资源管理器打开
      let operation = new DriverOperationDto();
      operation.driverId = driver.id;
      operation.id = 1;
      operation.name = "资源管理器打开"; 
      operation.mode = 1;
      operation.entry = "/app/resource-manager/#/home?path=${path}&type=${type}";
      driver.operation.push(operation);
    // 驱动-操作： 资源管理器打开
      let operation1 = new DriverOperationDto();
      operation1.driverId = driver.id;
      operation1.id = 2;
      operation1.data = {window:{ width: '600px',height: '500px'}};
      operation1.name = "属性"; 
      operation1.mode = 2;
      operation1.entry = "/app/resource-manager/#/property?path=${path}&type=${type}";
      driver.operation.push(operation1);

    // 驱动-加入
    opt.drivers.push(driver); 
    
    let map = new ruleDriverRelationDto();
        map.rule = /.*/;
        map.isDir = true;
        map.driverId = 1;
        opt.maps.push(map);

    let map2 = new ruleDriverRelationDto();
        map2.rule = /.*/;
        map2.isDir = false;
        map2.driverId = 1;
    opt.maps.push(map2);
     

// **** txt文本编辑器配置 ****

    // 驱动
    let driver2 = new DriverDto();
    driver2.id = 3;
    driver2.name = "txt文本编辑器";
    driver2.fileIconUrl = getFileDownloadUrl("Desktop/.ispace/images/drive_icons/txt.png");

    driver2.operation = []; 
    // 驱动-操作： txt文本编辑器打开
      let operation2 = new DriverOperationDto();
      operation2.driverId = driver2.id;
      operation2.id = 1;
      operation2.name = "打开"; 
      operation2.mode = 3;
      operation2.entry = "/app/text-editor/#/home?path=${path}&type=${type}";
      driver2.operation.push(operation2); 
    opt.drivers.push(driver2);
   // 映射
    let map3 = new ruleDriverRelationDto();
        map3.rule = /.*\.txt$/;
        map3.isDir = false;
        map3.driverId = 3;
    opt.maps.push(map3); 

 
    // 配置
    this.driveEngine.config(opt);  
  }

  getOperations(fileName: string,isDir: boolean) :DriverOperationDto[]{
    
    return this.driveEngine.getOperations(fileName,isDir);
  }

  execute(driverId:number,operationId:number, path:string,type:string, source?:any) { 
   let r = this.driveEngine.execute(driverId,operationId, {path,type,source}); 
   if(r)  {
    console.error('Error executing action:', r);
   } 
  }

}
